/* common function */
function hasClass(elements, cName) {
    return !!elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
};

function addClass(elements, cName) {
    if (!hasClass(elements, cName)) {
        elements.className += " " + cName;
    };
};

function removeClass(elements, cName) {
    if (hasClass(elements, cName)) {
        elements.className = elements.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), " ");
    };
};
var timer;
var zwfyReplayFn;
var zwfyUpdateRecord;
var flag = false;

//platform judge and share
var isWeChat = false;
var isQQ = false;
var isWeibo = false;
(function checkShare() {
    var ua = navigator.userAgent.toLowerCase();
    if (/micromessenger/.test(ua)) {
        isWeChat = true;
    } else if (/weibo/.test(ua)) {
        isWeibo = true;
    } else if (/qq/.test(ua)) {
        isQQ = true;
    }
})();

/*服务器游戏记录获取*/
var playerRecord = {};
playerRecord = window.parent.playerRecord;
if(playerRecord){
    var reScore = playerRecord.reScore;
    var bestRecord = playerRecord.bestRecord;
    var localStorage_itwlw = playerRecord.localStorage_itwlw;
}else{
    var reScore = null;
    var bestRecord = null;
    var localStorage_itwlw = null;
}

//var bestRecord = playerRecord.bestRecord;

//console.log(reScore,bestRecord,localStorage_itwlw);

(function(){
    var data = {};
    data['method'] = 'getRecord';
    window.parent.postMessage(data, "*");
})()

/*修改分享标题  OK*/
function changeShare() {
    //shareConfig.title = arguments[0];
    //shareData.title = arguments[0];
    var data = {};
    data['method'] = 'changeShare';
    data['title'] = arguments[0];
    window.parent.postMessage(data, "*");
    //console.log('changeShare in');
}

/*更新游戏记录接口*/
function updateRecord(score, Data, fn) {
    var data = {};
    data.method = 'updateRecord';
    data.score = score;
    data.data = Data;
    zwfyUpdateRecord = fn;
    data.fn = fn ? 1 : 0;
    window.parent.postMessage(data, "*");
    //console.log('updateRecord send msg');
}

/*更新游戏记录额外数据接口 OK*/
function updateStorage(Data) {
    var data = {};
    data.method = 'updateStorage';
    data.data = Data;
    window.parent.postMessage(data, "*");
    //console.log(data);
}

/*游戏结束调用 OK*/
function commonGameOver(score, propor, records, fn) {
    var  data= {};
    data.method = 'commonGameOver';
    data.score= score;
    data.propor = propor;
    data.records = records;
    zwfyReplayFn = fn;
    data.fn = fn ? 1 : 0;
    window.parent.postMessage(data, "*");
    console.log(data.score);
}


/*游戏排行榜接口 OK*/
function commonRank() {
    var data = {};
    data['method'] = 'commonRank';
    window.parent.postMessage(data, "*");
/*    console.log('commonRank')*/
}


/*打开非微信平台分享提示 OK*/
function showPlatformShare() {
    var data = {};
    data['method'] = 'showPlatformShare';
    window.parent.postMessage(data, "*");
/*    console.log('showPlatformShare in')*/
    event&&event.preventDefault();
    return true;
}
/*隐藏非微信平台分享提示 OK*/
function hidePlatformShare() {
    var data = {};
    data['method'] = 'hidePlatformShare';
    window.parent.postMessage(data, "*");
/*    console.log('hidePlatformShare in')*/
    event&&event.preventDefault();
    return true;
}


/*打开分享 OK*/
function showShareTip() {
/*    console.log('showShareTip');*/
    var data = {};
    data['method'] = 'showShareTip';
    window.parent.postMessage(data, "*");
}

/*监听再玩一次消息*/
window.addEventListener('message',function(e){
    switch(e.data.method){
        case 'replay':
            if(zwfyReplayFn && typeof(zwfyReplayFn)=="function"){
                zwfyReplayFn();
            }
            window.focus();
            break;
        case 'sendScore':
            zwfySendScore(e.data);
    }

/*    console.log(e.data);*/
});

function zwfySendScore(Data){
/*    console.log("get Data OK!"+Data.reScore+" "+Data.bestRecord+" " +Data.localStorage_itwlw)*/
    Score = Data.Score;
    bestRecord = Data.bestRecord;
    localStorage_itwlw = Data.localStorage_itwlw;
}

setTimeout(function(){
    window.onblur = function(){
        window.focus();
    }
},1000)
